package com.ruoyi.fn.controller;

import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.fn.service.IFinCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fn.domain.FinBudget;
import com.ruoyi.fn.service.IFinBudgetService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预算Controller
 * 
 * @author ruoyi
 * @date 2025-05-26
 */
@Controller
@RequestMapping("/fn/budget")
public class FinBudgetController extends BaseController
{
    private String prefix = "fn/budget";

    @Autowired
    private IFinBudgetService finBudgetService;

    @Autowired
    private IFinCategoryService finCategoryService;

    @RequiresPermissions("fn:budget:view")
    @GetMapping()
    public String budget()
    {
        return prefix + "/budget";
    }

    /**
     * 查询预算列表
     */
    @RequiresPermissions("fn:budget:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FinBudget finBudget)
    {
        startPage();
        List<FinBudget> list = finBudgetService.selectFinBudgetList(finBudget);
        return getDataTable(list);
    }

    /**
     * 导出预算列表
     */
    @RequiresPermissions("fn:budget:export")
    @Log(title = "预算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FinBudget finBudget)
    {
        List<FinBudget> list = finBudgetService.selectFinBudgetList(finBudget);
        ExcelUtil<FinBudget> util = new ExcelUtil<FinBudget>(FinBudget.class);
        return util.exportExcel(list, "预算数据");
    }

    /**
     * 新增预算
     */
    @RequiresPermissions("fn:budget:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("userString", ShiroUtils.getSysUser().toUserString());
        mmap.put("categories",finCategoryService.selectFinCategoryList(null));
        return prefix + "/add";
    }

    /**
     * 新增保存预算
     */
    @RequiresPermissions("fn:budget:add")
    @Log(title = "预算", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FinBudget finBudget)
    {
        finBudget.setUserId(ShiroUtils.getUserId());
        return toAjax(finBudgetService.insertFinBudget(finBudget));
    }

    /**
     * 修改预算
     */
    @RequiresPermissions("fn:budget:edit")
    @GetMapping("/edit/{budgetId}")
    public String edit(@PathVariable("budgetId") Long budgetId, ModelMap mmap)
    {
        FinBudget finBudget = finBudgetService.selectFinBudgetByBudgetId(budgetId);
        finBudget.setUserId(ShiroUtils.getUserId());
        mmap.put("finBudget", finBudget);
        mmap.put("userString", ShiroUtils.getSysUser().toUserString());
        mmap.put("categories",finCategoryService.selectFinCategoryList(null));
        return prefix + "/edit";
    }

    /**
     * 修改保存预算
     */
    @RequiresPermissions("fn:budget:edit")
    @Log(title = "预算", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FinBudget finBudget)
    {
        return toAjax(finBudgetService.updateFinBudget(finBudget));
    }

    /**
     * 删除预算
     */
    @RequiresPermissions("fn:budget:remove")
    @Log(title = "预算", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(finBudgetService.deleteFinBudgetByBudgetIds(ids));
    }
}
